/*
 * Copyright (c) 2012, Markus Weber All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer. Redistributions in binary form must reproduce the
 * above copyright notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the author nor the names of its contributors may be used to endorse or
 * promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package de.dfki.graph.index;

import de.dfki.graph.LabeledGraph;
import de.dfki.graph.Phytia;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import de.dfki.graph.db.GraphDatabase;
import de.dfki.graph.loader.GXLLoader;

public class Evaluation {
    private static final int MAX_NUMBER_OF_VERTICES = 30;
    /** Database */
    private static final String AIDS_DB = "src/main/resources/graphdb/AIDS/data/";
    private static final String Mutagenicity_DB = "src/main/resources/graphdb/Mutagenicity/data/";

    /**
     * @param args
     */
    public static void main(String[] args) {
	// BasicConfigurator.configure();
	GraphDatabase db = GraphDatabase.instance();
	// evaluationDatabase(db, new File(AIDS_DB), "symbol", "valence",
	// "AIDS");
	// evaluationDatabase(db, new File(Mutagenicity_DB), "chem", "valence",
	// "Mutagenicity");
	// evaluationRandom(db, 150, 1000, 1, "random-1");
	for (int i = 50; i <= 150; i += 25) {
	    evaluationRandom(db, i, 1000, 1, "random-" + i + "-1");
	    evaluationRandom(db, i, 1000, 5, "random-" + i + "-5");
	    evaluationRandom(db, i, 1000, 10, "random-" + i + "-10");
	}
	// evaluate runtime
	// evaluationRuntimeDatabase(db, "AIDS");

    }

    private static void evaluationRandom(GraphDatabase db, int max,
	    int numberofgraphs, int max_same, String name) {
	for (int i = 0; i < numberofgraphs; i++) {
	    db.addLabeledGraph(name,
		    Phytia.generateGraph("g" + i, max, max_same));

	}

	db.simulateIndexBuilding(name);

    }

    /**
     * evaluation of the runtime
     * 
     * @param db
     */
    private static void evaluationRuntimeDatabase(GraphDatabase db,
	    String dbname) {
	db.buildIndex(dbname);
    }

    /**
     * Evaluation database
     * 
     * @param db
     * @param dbfolder
     * @param labelattribute
     * @param edgeattribute
     * @param dbname
     */
    private static void evaluationDatabase(GraphDatabase db, File dbfolder,
	    String labelattribute, String edgeattribute, String dbname) {
	GXLLoader loader = new GXLLoader();
	List<String> todos = new ArrayList<String>();
	for (String f : dbfolder.list()) {
	    if (f.endsWith(".gxl")) {
		todos.add(f);
	    }
	}
	Collections.sort(todos);

	for (String f : todos) {
	    Set<LabeledGraph> set = loader.load(new File(dbfolder
		    + File.separator + f), labelattribute, edgeattribute);
	    for (LabeledGraph g : set) {
		if (g.numberOfVertices() > MAX_NUMBER_OF_VERTICES)
		    continue;
		db.addLabeledGraph(dbname, g);
	    }
	}
	System.out.println("Graph size: "
		+ db.getNumberOfUnindexedGraphs(dbname));
	db.simulateIndexBuilding(dbname);
    }

}
